//
//  CZStatusTableViewCell.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/14.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit
import SnapKit


let StatusTableViewCellMargin: CGFloat = 10
class CZStatusTableViewCell: UITableViewCell {
    var toolBarTop: Constraint?
    
    var statusViewModel: CZStatusViewModel? {
        didSet{
            //去除约束
            toolBarTop?.uninstall()
            originalView.statusViewModel = statusViewModel
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.statusViewModel = statusViewModel
                retweetView.hidden = false
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else
            {
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            toolBar.statusViewModel = statusViewModel
        }
        
        
    }
    private lazy var originalView: CZStatusOriginalView = CZStatusOriginalView()
    
    private lazy var toolBar: CZStatusToolBar = CZStatusToolBar()
    private lazy var retweetView: CZStatusRetweetView = CZStatusRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    设置子视图约束
    private func setupUI(){
           contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            //    make.height.equalTo(100)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        
        
        toolBar.snp_makeConstraints{ (make) -> Void in
            self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(40)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            
        }
    }
    
}
